﻿namespace Chain.Wechat
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Xml;

    public class IRequest
    {
        
        private string _CreateTime;
        
        private string _FromUserName;
        
        private string _MsgType;
        
        private string _ToUserName;
        
        private XmlDocument _XmlDoc;
        private RequestEventHandler _RequestEvent;

        public event RequestEventHandler RequestEvent
        {
            add
            {
                RequestEventHandler handler2;
                RequestEventHandler requestEvent = this._RequestEvent;
                do
                {
                    handler2 = requestEvent;
                    RequestEventHandler handler3 = (RequestEventHandler) Delegate.Combine(handler2, value);
                    requestEvent = Interlocked.CompareExchange<RequestEventHandler>(ref this._RequestEvent, handler3, handler2);
                }
                while (requestEvent != handler2);
            }
            remove
            {
                RequestEventHandler handler2;
                RequestEventHandler requestEvent = this._RequestEvent;
                do
                {
                    handler2 = requestEvent;
                    RequestEventHandler handler3 = (RequestEventHandler) Delegate.Remove(handler2, value);
                    requestEvent = Interlocked.CompareExchange<RequestEventHandler>(ref this._RequestEvent, handler3, handler2);
                }
                while (requestEvent != handler2);
            }
        }

        public IRequest(XmlDocument input)
        {
            this.XmlDoc = input;
            XmlNode root = this.XmlDoc.FirstChild;
            this.ToUserName = root["ToUserName"].InnerText;
            this.FromUserName = root["FromUserName"].InnerText;
            this.CreateTime = root["ToUserName"].InnerText;
            this.MsgType = root["MsgType"].InnerText;
        }

        public virtual string Response()
        {
            if (this._RequestEvent != null)
            {
                return this._RequestEvent();
            }
            return string.Empty;
        }

        public string CreateTime
        {
            
            get
            {
                return this._CreateTime;
            }
            
            set
            {
                this._CreateTime = value;
            }
        }

        public string FromUserName
        {
            
            get
            {
                return this._FromUserName;
            }
            
            set
            {
                this._FromUserName = value;
            }
        }

        public string MsgType
        {
            
            get
            {
                return this._MsgType;
            }
            
            set
            {
                this._MsgType = value;
            }
        }

        public string ToUserName
        {
            
            get
            {
                return this._ToUserName;
            }
            
            set
            {
                this._ToUserName = value;
            }
        }

        public XmlDocument XmlDoc
        {
            
            get
            {
                return this._XmlDoc;
            }
            
            set
            {
                this._XmlDoc = value;
            }
        }

        public delegate string RequestEventHandler();
    }
}

